package jp.sourceforge.akjrcp.akj_betas.actions;

import com.akjava.lib.swt.log.ExecuteLog;
import com.akjava.lib.swt.log.ExecuteLogTableViewer;
import com.akjava.lib.swt.ui.dialog.TextDialog;
import com.akjava.lib.swt.util.ClipboardUtil;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.commons.ui.widget.LayoutDataBuilder;
import jp.sourceforge.akjrcp.commons.ui.widget.WidgetBuilder;
import jp.sourceforge.akjrcp.util.SelectionUtils;



/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class ShowLogAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	/**
	 * The constructor.
	 */
	public ShowLogAction() {
		
	}

	/**
	 * The action has been activated. The argument of the
	 * method represents the 'real' action sitting
	 * in the workbench UI.
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {
		
		/*
		LogDialog dialog=new LogDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
				Messages.getString("log.title"),null,Akj_betasPlugin.getDefault().getProcessPrinter().getLog(),0,new String[]{"OK"},0);
		*/
	

		//dialog.open();
		LogWindow window=new LogWindow(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),Messages.getString("log.title"),
				Akj_betasPlugin.getDefault().getProcessPrinter().getLog());
	
		window.open();
	}

	public class LogWindow extends Window{
		private Text text;
		private String message="";
		private String title;
		public LogWindow(Shell parentShell,String title,String message) {
			super(parentShell);
			this.title=title;
			this.message=message;
		}
		protected void configureShell(Shell newShell) {
			super.configureShell(newShell);
			//
		}
		protected Control createContents(Composite window) {
			getShell().setText(title);
			Composite parent=WidgetBuilder.createGridLayoutComposite(window);
			parent.setLayoutData(new GridData(GridData.FILL_BOTH));
			Composite dummy=WidgetBuilder.createGridLayoutComposite(parent);
			dummy.setLayoutData(LayoutDataBuilder.hintGridData(-1, 250));
			
			final ExecuteLogTableViewer execLogViewer=new ExecuteLogTableViewer(dummy);
			execLogViewer.setInput(Akj_betasPlugin.getDefault().getExecuteLogs());
			execLogViewer.getTable().setLayoutData(LayoutDataBuilder.fillBothGridData());

			GridData data=new GridData();//for vista//TODO still here is problem
			
		    data.heightHint=250;
		    
		    data.grabExcessVerticalSpace=true;
		    data.widthHint=640;
		    data.grabExcessHorizontalSpace=true;
		    
		    
		
		    
		   final CTabFolder tab=new CTabFolder(parent,SWT.NULL);
		   tab.setLayoutData(new GridData(GridData.FILL_BOTH));
		   CTabItem defaultLog=new CTabItem(tab,SWT.NULL);
		   defaultLog.setText(Messages.getString("log.message"));
		   tab.setSelection(0);
		   
		   Composite execLogs=WidgetBuilder.createGridLayoutComposite(tab);
		   Composite controls2=WidgetBuilder.createGridLayoutComposite(execLogs, SWT.NULL, 5, false);
		   
		   
		   final Text  execText = WidgetBuilder.createTextAreaReadOnly(execLogs, "");
		  
		    Button reportButton2=WidgetBuilder.createButton(controls2, Messages.getString("log.report"));
			   
		    reportButton2.addSelectionListener(new SelectionListener(){

				public void widgetDefaultSelected(SelectionEvent e) {
						
				}

				public void widgetSelected(SelectionEvent e) {
					String log=execText.getText();
					log=log.replaceAll(System.getProperty("user.name"), "*****");
					ClipboardUtil.setClipboard(getShell().getDisplay(), log);
				}});
		    
		    Button updateButton2=new Button(controls2,SWT.NULL);
		    updateButton2.setText(Messages.getString("log.update"));
		    updateButton2.addSelectionListener(new SelectionListener(){

				public void widgetDefaultSelected(SelectionEvent e) {
						
				}

				public void widgetSelected(SelectionEvent e) {
					execText.setText("");
					execLogViewer.refresh();
				}});
		   
		   execText.setLayoutData(LayoutDataBuilder.hintAndBothGridData(640, 250));
		   CTabItem execLog=new CTabItem(tab,SWT.NULL);
		   execLog.setControl(execLogs);
		   execLog.setText(Messages.getString("log.execlog"));
		   
		   execLogViewer.addSelectionChangedListener(new ISelectionChangedListener(){

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				
				Object[] obj=SelectionUtils.getSelectObject(event.getSelection());
				for (int i = 0; i < obj.length; i++) {
					execText.setText(((ExecuteLog)obj[i]).getLogBuffer().toString());
					   tab.setSelection(1);//active exec log
					break;
				}
			}
			   
		   });
		   

		   //dummy select
		   //System.out.println(execLogViewer.getElementAt(0));
		 //  execLogViewer.setSelection(new StructuredSelection(execLogViewer.getElementAt(0)),true);
		   
		    
		    Composite defaultLogs=WidgetBuilder.createGridLayoutComposite(tab);
		    defaultLogs.setLayoutData(new GridData(GridData.FILL_BOTH));
			defaultLog.setControl(defaultLogs);
			   
		    Composite controls=WidgetBuilder.createGridLayoutComposite(defaultLogs, SWT.NULL, 5, false);
		  
		    
		    Button clearButton=WidgetBuilder.createButton(controls, Messages.getString("log.clear"));
		    clearButton.addSelectionListener(new SelectionListener(){
		    	public void widgetDefaultSelected(SelectionEvent e) {
						
				}
				public void widgetSelected(SelectionEvent e) {
					Akj_betasPlugin.getDefault().getProcessPrinter().clear();
					text.setText("");
				}});
		    
		    
		    Button reportButton=WidgetBuilder.createButton(controls, Messages.getString("log.report"));
		   
		    reportButton.addSelectionListener(new SelectionListener(){

				public void widgetDefaultSelected(SelectionEvent e) {
						
				}

				public void widgetSelected(SelectionEvent e) {
					String log=text.getText();
					log=log.replaceAll(System.getProperty("user.name"), "*****");
					ClipboardUtil.setClipboard(getShell().getDisplay(), log);
				}});
		    
		    Button updateButton=new Button(controls,SWT.NULL);
		    updateButton.setText(Messages.getString("log.update"));
		    updateButton.addSelectionListener(new SelectionListener(){

				public void widgetDefaultSelected(SelectionEvent e) {
						
				}

				public void widgetSelected(SelectionEvent e) {
					text.setText(Akj_betasPlugin.getDefault().getProcessPrinter().getLog());
				}});
		    
		    
		    text = WidgetBuilder.createTextAreaReadOnly(defaultLogs, message);
		    text.setLayoutData(LayoutDataBuilder.hintAndBothGridData(640, 250));
 
			
			
			
			
			//create close button
			final Button close=WidgetBuilder.createButton(parent, Messages.getString("close"));
			close.setLayoutData(LayoutDataBuilder.alignCenterData());
			close.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					LogWindow.this.close();
				}
			});
			
			

			
			return parent;
		}
	}
	
	/**
	 * Selection in the workbench has been changed. We 
	 * can change the state of the 'real' action here
	 * if we want, but this can only happen after 
	 * the delegate has been created.
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/**
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
	}

	/**
	 * We will cache window object in order to
	 * be able to provide parent shell for the message dialog.
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
	
	/**
	 * @deprecated
	 * @author aki
	 *
	 */
	public class LogDialog extends MessageDialog{

		private Text text;
		private String message;
		public LogDialog(Shell parentShell, String dialogTitle, Image dialogTitleImage, String dialogMessage, int dialogImageType, String[] dialogButtonLabels, int defaultIndex) {
			super(parentShell, dialogTitle, dialogTitleImage, "",
					dialogImageType, dialogButtonLabels, defaultIndex);
			message=dialogMessage;
			// TODO Auto-generated constructor stub
		}
		
		  protected Control createCustomArea(Composite parent) {
			  
			    GridData data=new GridData();//for vista//TODO still here is problem
			    data.heightHint=400;
			    
			    data.grabExcessVerticalSpace=true;
			    data.widthHint=640;
			    data.grabExcessHorizontalSpace=true;
			    Composite root=new Composite(parent,SWT.NULL);
			    root.setLayoutData(new GridData(GridData.FILL_BOTH));
			    root.setLayout(new GridLayout(1,false));
			    Composite controls=new Composite(root,SWT.NULL);
			    controls.setLayout(new RowLayout());
			    Button clearButton=new Button(controls,SWT.NULL);
			    clearButton.setText(Messages.getString("log.clear"));
			    clearButton.addSelectionListener(new SelectionListener(){

					public void widgetDefaultSelected(SelectionEvent e) {
							
					}

					public void widgetSelected(SelectionEvent e) {
						Akj_betasPlugin.getDefault().getProcessPrinter().clear();
						text.setText("");
					}});
			    
			    
			    Button reportButton=new Button(controls,SWT.NULL);
			    reportButton.setText(Messages.getString("log.report"));
			    reportButton.addSelectionListener(new SelectionListener(){

					public void widgetDefaultSelected(SelectionEvent e) {
							
					}

					public void widgetSelected(SelectionEvent e) {
						String log=text.getText();
						log=log.replaceAll(System.getProperty("user.name"), "*****");
						ClipboardUtil.setClipboard(getShell().getDisplay(), log);
					}});
			    
			    Button updateButton=new Button(controls,SWT.NULL);
			    updateButton.setText(Messages.getString("log.update"));
			    updateButton.addSelectionListener(new SelectionListener(){

					public void widgetDefaultSelected(SelectionEvent e) {
							
					}

					public void widgetSelected(SelectionEvent e) {
						text.setText(Akj_betasPlugin.getDefault().getProcessPrinter().getLog());
					}});
			    
			    Composite panel=new Composite(root,SWT.NULL);
			    panel.setLayout(new FillLayout());
			    //panel.setLayout(new RowLayout());
			    text = new Text(panel,SWT.BORDER|SWT.READ_ONLY|SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL);
				
			   panel.setLayoutData(data); 
			   
		        text.setText(message);
		        
		        
		       
		      
		       // scroll.setLayoutData(data);
		       // scroll.setContent(text);
		       // scroll.setMinSize(320,240);
		        //scroll.setMinSize(parent.getClientArea().width,parent.getClientArea().height);
		        //scroll.setExpandHorizontal(true);
				//scroll.setExpandVertical(true);
		        //return this.text;
		        return panel;
		    }

	

	}
}